package com.banciyuan.bcywebview.biz.detail.complex.helper;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall on 2016/8/10.
 */
public class ComplexHelper {

    private Context mContext;
    private RequestQueue mQueue;
    private DetailType mDetailType;
    private boolean isLiking;

    public ComplexHelper(Context mContext, DetailType mDetailType) {
        this.mContext = mContext;
        this.mQueue = VolleyQueue.getRquest(mContext);
        this.mDetailType = mDetailType;
    }

    public void setmDetailType(DetailType mDetailType) {
        this.mDetailType = mDetailType;
    }

    public void getDetailData(final ComplexHelperInter helperInter) {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    switch (jsonObject.getInt(HttpUtils.UPLOAD_STATUS)) {
                        case MyConstants.DATA_NOTEXIT:
                            helperInter.fail(MyConstants.DATA_NOTEXIT);
                            break;
                        case MyConstants.NOTEXIT:
                            helperInter.fail(MyConstants.DATA_NOTEXIT);
                            break;
                        case MyConstants.NEED_FANS:
                            helperInter.succ(MyConstants.NEED_FANS, jsonObject.getString(HttpUtils.UPLOAD_DATA));
                            break;
                        case MyConstants.NEED_LOGIN:
                            helperInter.fail(MyConstants.NEED_LOGIN);
                            break;
                        case MyConstants.WORK_LOKED:
                            helperInter.fail(MyConstants.WORK_LOKED);
                            break;
                        case MyConstants.SOMETINGS_WRONG:
                            helperInter.fail(MyConstants.SOMETINGS_WRONG);
                            break;
                        case MyConstants.PERSON_NOT_EXIST:
                            helperInter.fail(MyConstants.SOMETINGS_WRONG);
                            break;
                        default:
                            helperInter.succ(1, jsonObject.getString(HttpUtils.UPLOAD_DATA));
                            break;
                    }
                } catch (JSONException e) {
                    helperInter.fail(0);
                }

            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                helperInter.fail(0);
            }
        };
        CommonApiUtils.getDetailData(mContext, mQueue, onSuccess, onError, mDetailType);
    }

    public void getCommentData(int pageNum, final ComplexHelperInter helperInter) {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        JSONObject jsonObject = new JSONObject(response);
                        helperInter.succ(1, jsonObject.getString(HttpUtils.UPLOAD_DATA));
                    } else {
                        helperInter.fail(0);
                    }
                } catch (JSONException e) {
                    helperInter.fail(0);
                }

            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                helperInter.fail(0);
            }
        };
        CommonApiUtils.getCommentData(mContext, mQueue, onSuccess, onError, mDetailType, pageNum);
    }

    public void doLike(final Complex complex, final TimelineLikeHelper.TimelineSuccess timelineSuccess) {
        if (isLiking) return;
        isLiking = true;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    timelineSuccess.onTimelineSuccess();
                }
                isLiking = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isLiking = false;
            }
        };

        if (complex.isHave_ding()) {
            CommonApiUtils.dislike(mContext, mQueue, onSuccess, onError, mDetailType);
        } else {
            CommonApiUtils.doLike(mContext, mQueue, onSuccess, onError, mDetailType);
        }
    }


}
